Script writing, like any form of computer programming, requires planning. Think, in general terms, of the steps that will be required to complete the task. Remember to include error checking in your planning, think of where possible errors might be encountered and how you will overcome them. Another good method for developing a script is to take an existing one and adapt it to your needs. There are a number of very well written scripts available for free from the PocoMail website. Choose a script that has the basics of what you want to do and customize it from there. Remember to save it under a different name when you start editing it.
Another very useful technique in script writing is to only program small segments at any one time, inserting MessageBox commands to display the results of each step. If the script is composing a message to be sent and you want to view the contents of the message in order to debug it then the EditMessage command is particularly useful.
Two techniques that will assist you in reading a script are:
For the purpose of this tutorial we will create two scripts, one that will notify people who send us email that we are on vacation and the other to tag all annotated messages in a mailbox so they can be displayed using "Show only" bar.
This script will notify people who send us email that we are on vacation. We don't want to confirm our email address to spammers so we will restrict the replies to people whose email addresses are in one of our address books. We also want to keep track of any messages which we have sent vacation replies to.
This script must extract the email address from the sender (the "To:" header in the incoming message), check the address book(s) to see if it is present and, if the address is found, format and send an automated reply to the sender.
{ Script: VacationReply
{ Created: June 13, 2001
{ Author: Michael Motek
{ Purpose: Part of the PocoScript tutorial. This script will send a message informing people you are on vacation.
{ Mode: This script is designed to run against incoming messages.
{ Modification log:
The next step is to add statements to handle any errors that may be generated. Since this script is designed to run unattended we do not want a message box to be displayed when an error in encountered. Thus we do not use the OnErrorMessage variable. The code to handle errors consists of a single statement immediately following the initial comments:
Set $OnErrorGoTo ErrorHandler
Exit
:ErrorHandler
Exit
Now we have to allow for customization of the reply text. This is done by adding statements that will run when the script is run in "Setup" mode. The text for the reply will come from a file named "VacationReply.txt". Add the following statements after the "Set" statement for the $OnErrorGoTo system variable:
External $userfile1 "Reply text" "VacationReply.txt"
Set $replyfile $mailpath
AddStrings $replyfile "VacationReply.txt"
OpenBody $replytext $replyfile
To test to see that we have read the file correctly we add a MessageBox statement:
MessageBox $replytext
{ Get the sender's email address
ReadHeader $from "From:" %message
ExtractEMail $from
To verify the script is picking up the correct information add a MessageBox statement and test the script by running it against a message in one of your mailboxes.
{ Check to see if the sender's email address is present in one of our address books.
IsAddressPresent &sw $from
If &sw Then Continue
MessageBox "Not in any address book, no reply will be sent"
Exit
:Continue
MessageBox "In address book, a reply will be sent"
{ Create the reply and add the necessary headers.
CreateMessage %m
AddTo %m $from
Now we want to assign a subject to the email. We don't want the person receiving the note to think we are actually replying to it so we use a static subject line rather than copying the subject line from their note: The AddHeader command accomplishes this task.
AddHeader %m "Subject:" "Vacation reply"
We also need to set put our email address into the "From" header in the reply. This can be extracted from the settings for the current Poco account using the following commands:
ReadEmail $from
AddHeader %m "From:" $from
For debugging purposes at this point you would use the EditMessage command:
EditMessage %m
{ Assign the body from the template file.
AssignBody %m $replytext
{ Save the message to the out box. Note: The "Send queued messages when checking mail"
{ option must be selected in the program options in order for the message to be sent.
AppendToFile True
SaveMessage %m "out.mbx"
MarkMessage %message 8
{ Script: VacationReply { Created: June 13, 2001 { Author: Michael Motek { Purpose: Part of the PocoScript tutorial. This script will send a message informing people you are on vacation. { Mode: This script is designed to run against incoming messages. { Modification log Set $OnErrorGoTo "ErrorHandler" External $userfile1 "Reply text" "VacationReply.txt" { Read the reply text Set $replyfile $mailpath AddStrings $replyfile "VacationReply.txt" OpenBody $replytext $replyfile { Get the sender's email address ReadHeader $from "From:" %message ExtractEMail $from MessageBox $from { Check to see if the sender's email address is present in one of our address books. IsAddressPresent &sw $from "*" If &sw Then Continue Exit :Continue { Create the reply and add the necessary headers. CreateMessage %m AddTo %m $from AddHeader %m "Subject:" "Vacation reply" ReadEmail $from AddHeader %m "From:" $from { Assign the body from the template file. AssignBody %m $replytext { Save the message to the out box. Note: The "Send queued messages when checking mail" { option must be selected in the program options in order for the message to be sent. AppendToFile True SaveMessage %m "out.mbx" MarkMessage %message 8 Exit :ErrorHandler Exit
All that remains is testing the script to ensure it is functioning correctly. We have tested various steps the script uses so it is simply a matter of sending ourselves a test message and running the script against that message. Once we have done that the script will be ready to install. Once you have received the test message run the script against it using the technique described to run a script against selected messages.
Once you have confirmed the script is working properly it is ready to install. When you are about to go on vacation you would enable the script by configuring it to run against incoming messages. Remember to check the reply text to ensure it is what you want to send. Also, make a note to disable the script when you return from vacation.
We have used the message annotation feature to annotate messages in our mailboxes. We would like to be able to filter the mailbox index to include only those messages that are annotated, unfortunately Poco doesn't have an option to do this. We can overcome this by writing a script that will Tag all messages in the mailbox that have an annotation.
{ TagAnnotatedMessages - Select (tag) all messages that have an annotation
{ (or whose annotation contains a user specified phrase)
{ Author: Michael Motek - June 19, 2001
{ Usage: This script is designed to be run against selected messages.
{ Purpose: This script will select (tag) all messages that have annotation attached
{ to them or those with a specific phrase in the annotation.
{ Once tagged the Poco "Show Only" bar can be used to display only those messages.
ReadHeader $note "X-Poco-Annotation:" %message
If $note = "" Then NoAnnotation
Exit
:NoAnnotation
TagMessage %message
UntagMessage %message
{ TagAnnotatedMessages - Select (tag) all messages that have an annotation { (or whose annotation contains a user specified phrase) { Author: Michael Motek - June 19, 2001 { Usage: This script is designed to be run against selected messages. { Purpose: This script will select (tag) all messages that have annotation attached { to them or those with a specific phrase in the annotation. { Once tagged the Poco "Show Only" bar can be used to display only those messages. ReadHeader $note "X-Poco-Annotation:" %message If $note = "" Then NoAnnotation TagMessage %message Exit :NoAnnotation UntagMessage %message
Proper error handling is important when scripts are to be run unattended or when they are written for other people to use. In these situations it is very important to record the details of the error in a file for future reference. This section of the tutorial will demonstrate a techniques that you can use to accomplish this.
The first decision that must be made is the location and name of the error log. We will put the error long into the user's temporary directory and name it "pocoscripterrorlog.txt". We want to produce a cumulative log of the errors rather than only the last one so we will also test to see if the file exists, if it does we will append new entries to it, otherwise a new file will be created.
The script name, error number, line number on which the error occurred, date and time the error occurred should be recorded.
The commands that will accomplish this are:
{ Log any errors to file "pocoscripterrorlog.txt" in the user's temporary file directory.
{ Construct the information to log
GetDate $date
GetTime $time
AddStrings $ErrMsg "Error " #ErrorResult " occurred at " $time " on " $date " on line " #ErrorLine " in VacationReply"
:LogError
{ Construct the name and path for the error log file.
Set $errfile $temppath
AddStrings $file "pocoscripterrorlog.txt"
{ Test to see if the error log file exists
FileExists &sw $file
AppendToFile &sw
{ Save the message to the log
SaveBody $ErrMsg $errfile
Notes: